package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.Thread.UserThreadLocal;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.ElderDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.FamilyMemberElderService;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.ElderFamilyInfoVo;
import com.zzyl.nursing.vo.FamilyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements FamilyMemberElderService {
   @Autowired
   private ElderMapper elderMapper;
   @Autowired
   private FamilyMemberElderMapper familyMemberElderMapper;
   @Autowired
   private IDeviceService deviceService;

    /**
     * 绑定老人信息
     *
     * @param elderDto
     */
    @Override
    public void addMember(ElderDto elderDto) {

        //根据老人id查询老人信息
        Elder elder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getIdCardNo,elderDto.getIdCard()));
        if (ObjectUtil.isEmpty( elder)) {
            throw new RuntimeException("没有该老人信息");
        }

        //获取当前线程的用户id
        Long userId = UserThreadLocal.getUserId();
        //判断老人是否被绑定过
        FamilyMemberElder familyMemberElders = familyMemberElderMapper.selectOne(Wrappers.<FamilyMemberElder>lambdaQuery()
                .eq(FamilyMemberElder::getElderId,elder.getId())
                .eq(FamilyMemberElder::getFamilyMemberId,userId));
        if (ObjectUtil.isNotEmpty(familyMemberElders)) {
            throw new RuntimeException("该老人已被绑定");
        }
        //将老人的信息保存到数据库中
        FamilyMemberElder familyMemberElder = FamilyMemberElder.builder()
                .elderId(elder.getId())
                .familyMemberId(userId)
                .build();
        familyMemberElder.setRemark(elderDto.getRemark());

        this.save(familyMemberElder);
    }

    /**
     * 查询家人列表
     *
     * @ return
     */
    @Override
    public List<ElderFamilyInfoVo> getMyMember() {
        //查询家人列表
        List<ElderFamilyInfoVo> list = familyMemberElderMapper.getMyMember();
        return list;
    }

    /**
     * 查询家人列表
     *
     * @ param familyVo
     */
    @Override
    public List<FamilyVo> listByPage() {
        Long userId = UserThreadLocal.getUserId();
        FamilyMemberElder familyMemberElder = this.getOne(Wrappers.<FamilyMemberElder>lambdaQuery().eq(FamilyMemberElder::getFamilyMemberId, userId));
        Long elderId = familyMemberElder.getElderId();
        //1.根据存储的老人id查询到老人的相关信息
        Elder elder = elderMapper.selectElderById(elderId);
        if (ObjectUtil.isEmpty(elder)) {
            throw new RuntimeException("没有该老人信息");
        }
        elder.setImage(elder.getImage());
        //获取到老人的床位信息
        elder.setBedNumber(elder.getBedNumber());
        //获取到老人佩戴的设备
        Device device = deviceService.getOne(Wrappers.<Device>lambdaQuery()
                .eq(Device::getRemark, elder.getName()));
        if (ObjectUtil.isEmpty(device)){
            throw new RuntimeException("该老人没有佩戴设备");
        }
        device.setIotId(device.getIotId());
        device.setDeviceName(device.getDeviceName());
        device.setProductKey(device.getProductKey());
        //封装数据
        List list = new ArrayList<>();
        list.add(elder);
        list.add(device);
        return list;
    }
}
